const LogModel = require('../../model/Log');
const moment = require('moment')
const Activity = require('../../model/Activity')
const Store = require('../../model/Store')
const async = require('async')

module.exports = {

    changeActivity: async(req, res, next) => {
        try {
            if (req.body.id) {
                let activity = await Activity.findOne({ _id: req.body.id }).exec()
                activity.store = req.body.store,
                    activity.title = req.body.title,
                    activity.subtitle = req.body.subtitle,
                    activity.updatedAt = new Date(),
                    activity.images = req.body.images
                activity.detail = req.body.detail
                activity.refLink = req.body.refLink
                activity.imageUrl = req.body.imageUrl
                activity.showImageLarge = req.body.showImageLarge,
                activity.showImageSmall = req.body.showImageSmall
                await activity.save()
                let oldStoreList = (await Store.find({ recommendActivity: { $in: req.body.id }, isDelete: 0 }, '_id').exec()).map(e => { return String(e._id) })
                async.each(req.body.store, async function(item, callback) {
                    let index = oldStoreList.indexOf(item)
                    if (index == -1) {
                        let store = await Store.findOne({ _id: item, isDelete: 0 }).exec()
                        store.recommendActivity.push(req.body.id)
                        store.markModified('recommendActivity')
                        await store.save()
                    }
                })
                async.each(oldStoreList, async function(item, callback) {
                    let index = req.body.store.indexOf(item)
                    if (index == -1) {
                        let store = await Store.findOne({ _id: item, isDelete: 0 }).exec()
                        let storeIndex = store.recommendActivity.indexOf(req.body.id)
                        store.recommendActivity.splice(storeIndex, 1)
                        store.markModified('recommendActivity')
                        await store.save()
                    }
                })
                return res.ok('修改活动成功', activity)
            } else if (!req.body.id) {
                let newActivity = new Activity({
                    store: req.body.store,
                    title: req.body.title,
                    subtitle: req.body.subtitle,
                    updatedAt: new Date(),
                    images: req.body.images,
                    detail: req.body.detail,
                    isDelete: 0,
                    refLink: req.body.refLink,
                    imageUrl: req.body.imageUrl,
                    showImageLarge : req.body.showImageLarge,
                    showImageSmall : req.body.showImageSmall
                })
                await newActivity.save()
                async.each(req.body.store, async function(item, callback) {
                    let store = await Store.findOne({ _id: item, isDelete: 0 }).exec()
                    store.recommendActivity.push(newActivity._id)
                    store.markModified("recommendActivity")
                    await store.save()
                })
                return res.ok('创建活动成功', newActivity)
            }
        } catch (e) {
            next(e)
        }
    },


    deleteActivity: async(req, res, next) => {
        try {
            let activity = await Activity.findOne({ _id: req.body.id }).exec()
            activity.isDelete = 1
            await activity.save()
            return res.ok('删除活动成功')
        } catch (e) {
            next(e)
        }
    },


    getOneActivity: async(req, res, next) => {
        try {
            let activity = await Activity.findOne({ _id: req.body.id, isDelete: 0 }).populate('store').exec()
            return res.ok('查询活动成功', activity)
        } catch (e) {
            next(e)
        }
    },

    getActivitylist: async(req, res, next) => {
        try {
            if (req.body.startDate) {
                var startDate = new Date(req.body.startDate)
            }
            if (req.body.endDate) {
                var endDate = new Date(req.body.endDate)
            }
            let option = {}
            option.isDelete = 0
            if (req.body.name) {
                option.title = new RegExp(req.body.name, 'i')
            }
            if (req.body.isDelete) {
                option.isDelete = req.body.isDelete
            }
            if (startDate && !endDate) {
                option.createdAt = { $gte: startDate }
            } else if (!startDate && endDate) {
                option.createdAt = { $lte: endDate }
            } else if (startDate && endDate) {
                option.$and = [{ createdAt: { $gte: startDate } }, { createdAt: { $lte: endDate } }]
            }
            if (req.session.user.store.length != 0) {
                option.store = req.session.user.store
            } else if (req.body.store) {
                option.store = req.body.store
            }
            let count = await Activity.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await Activity.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    }

};